package com.example.librarymanagementsystembackend;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.util.Arrays;


@SpringBootApplication(exclude = {SecurityAutoConfiguration.class}) // 排除 Spring Security 自动配置
@MapperScan("com.example.librarymanagementsystembackend.*.mapper") // 配置Mapper接口包路径
@PropertySource(value="file:../localConfig/database.properties") //, ignoreResourceNotFound = true) // 项目外部配置源
@EnableAspectJAutoProxy // 支持 AspectJ AOP
public class LibraryManagementSystemBackendApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(LibraryManagementSystemBackendApplication.class, args);
    }

    @Bean // 配置数据源
    @ConditionalOnProperty(prefix = "datasource", name = {"url", "username", "password", "driver"})
    public DataSource dataSource(
            @Value("${datasource.url}") String url,
            @Value("${datasource.username}") String username,
            @Value("${datasource.password}") String password,
            @Value("${datasource.driver}") String driver) {

        return DataSourceBuilder.create()
                .url(url)
                .username(username)
                .password(password)
                .driverClassName(driver)
                .build();
    }
}
